import { Bi } from "@/module-00-codebase/pkg-01-container";
import { User } from "@/module-00-codebase/pkg-08-entity/user";

export class PermManager {
  public has(permRoute: string): boolean {
    if (!user) {
      return false;
    } else if ((user.roles as any[]).some(role => role.name === ADMIN_ROLE_NAME)) {
      return true;
    } else {
      return routeSet.has(permRoute);
    }
  }
  public async refresh() {
    routeSet.clear();
    user = await Bi.userManager.getUser();
    if (!!user) {
      const res = await Bi.rpc.send(Bi.utils.platingValue({ resourceName: "innerPerm", action: "getlist", "data.criteria.projections[0].property": "route" }));
      const list = res?.data?.list || [];
      list.forEach((item: any) => routeSet.add(item.values[0]));
    }
  }
}
export const initializing = async (pm: PermManager) => {
  pm.refresh();
};
export const dependencies = () => ["UserManager", "Utils"];
let user: User | null;
const routeSet = new Set<string>();
const ADMIN_ROLE_NAME = "ADMIN-ROLE";
